package com.dragons.spring5_webflux.handler;

import com.dragons.spring5_webflux.entities.User;
import com.dragons.spring5_webflux.service.UserService;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.BodyInserters.fromObject;

public class UserHandler {
    private final UserService userService;
    public UserHandler(UserService userService) {
        this.userService = userService;
    }
    //根据 id 查询
    public Mono<ServerResponse> getUserById(ServerRequest request) {
        int userId = Integer.valueOf(request.pathVariable("id"));//获取 id 值
        Mono<ServerResponse> notFound = ServerResponse.notFound().build();//空值处理
        //调用 service 方法得到数据
        Mono<User> userMono = this.userService.getUserById(userId);
        //把 userMono 进行转换返回  使用 Reactor 操作符 flatMap
        return
                userMono
                        .flatMap(person ->
                                ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
                                        .body(fromObject(person)))
                        .switchIfEmpty(notFound);
    }
    //查询所有
    public Mono<ServerResponse> getAllUsers() {
        Flux<User> users = this.userService.getAllUser();//调用 service 得到结果
        return
                ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(users,User.class);
    }
    //添加
    public Mono<ServerResponse> saveUser(ServerRequest request) {
        Mono<User> userMono = request.bodyToMono(User.class);//得到 user 对象
        return
                ServerResponse.ok().build(this.userService.saveUserInfo(userMono));
    }

}